{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用selenium框架爬取网站内容\n",
    "# 初级版：爬取网站页面的静态内容\n",
    "# chromedrive下载网址：https://npm.taobao.org/mirrors/chromedrive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需库\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化虚拟浏览器并返回driver\n",
    "def Driver():\n",
    "    # 虚拟浏览器的参数设置\n",
    "    chrome_options=Options()\n",
    "    chrome_options.add_argument('--headless')   #无头chrome\n",
    "    prefs={'profile.managed_default_content_settings.images':2}   #限制加载图片\n",
    "    chrome_options.add_experimental_option(\"prefs\",prefs)\n",
    "    # 原文用的chrome_option,现在参数改成了option\n",
    "    driver=webdriver.Chrome(r\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe\",options=chrome_options)\n",
    "    return driver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据存储\n",
    "def save(n,h):\n",
    "    f=open('./result_基础版.csv','a',newline='',encoding='utf-8-sig')\n",
    "    writer = csv.writer(f)\n",
    "    lis=[n,h]\n",
    "    writer.writerow(lis)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用DOM节点获取网页内容\n",
    "def obtain_data(mainurl):\n",
    "    driver=Driver()\n",
    "    driver.get(mainurl)  #打开网址\n",
    "    driver.set_page_load_timeout(10)   #等待时间，根据网速情况自行设置\n",
    "    nameList=driver.find_elements_by_css_selector('.news-wrap>.news-list>li>a')\n",
    "    for i in range(0,len(nameList)):\n",
    "        name=driver.find_elements_by_css_selector('.news-wrap>.news-list>li>a')[i].text\n",
    "        herf=driver.find_elements_by_css_selector('.news-wrap>.news-list>li>a')[i].get_attribute('href')\n",
    "        print(name,herf)\n",
    "        save(name,herf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "    mainurl='http://rettc.ustb.edu.cn/zh/index.html'   # 要爬取的网站\n",
    "    f=open('./result_基础版.csv','w')    \n",
    "    save('新闻名称','新闻链接')\n",
    "    obtain_data(mainurl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中国-加纳/赞比亚可再生能源技术转移项目2020年全球指导委员会会议在京召开 http://rettc.ustb.edu.cn/zh/newsdetails.html?6ed4dea8ea4d480db3e38ae1c5a4329f\n",
      "中国21世纪议程管理中心招聘技术转移南南合作中心工作人员的公告 http://rettc.ustb.edu.cn/zh/newsdetails.html?b1a822e44e0149e0ad79a6f9bf86a5bc\n",
      "技术转移南南合作专家研讨会在京举行 http://rettc.ustb.edu.cn/zh/newsdetails.html?ef4a39a828f345c5a0fd3eaddecb75e5\n",
      "乌干达清洁炉灶联盟执行主任George Sizoomu一行访问21世纪中心技术转移南南合作中心 http://rettc.ustb.edu.cn/zh/newsdetails.html?073aaadfdb4a4f03931a968ad4d1f1b8\n",
      "21世纪中心赴长沙参加亚太绿色低碳发展高峰论坛并开展科研项目管理调研 http://rettc.ustb.edu.cn/zh/newsdetails.html?28fa347007f24c85ac19e90c6a4e0658\n",
      "潘基文题词祝贺 “技术转移南南合作中心” 成立 http://rettc.ustb.edu.cn/zh/newsdetails.html?440ba8e95dda4bd4a60eabd2941d4afd\n",
      "技术转移南南合作中心在京正式挂牌 http://rettc.ustb.edu.cn/zh/newsdetails.html?c937cbca8bce42888c2e0127576bb495\n",
      "南南合作清洁炉灶技术培训暨发展中国家清洁炉灶产业交流会在烟台举行 http://rettc.ustb.edu.cn/zh/newsdetails.html?e7949066a31c49b3a8f22fd04b616067\n"
     ]
    }
   ],
   "source": [
    "main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
